package sequence;

/**
 * @Author: LDeng
 * @Date: 2021-05-07 10:03
 */
public class BruteForce01 {

    public static int indexOf(String text, String pattern) {
        //前置判断
        if (text == null || pattern == null) return -1;
        char[] textChars = text.toCharArray();
        int tlen = textChars.length;
        if (tlen == 0) return -1;
        char[] patternChars = pattern.toCharArray();
        int plen = patternChars.length;
        if (plen == 0) return -1;
        if (tlen < plen) return -1;

        int pi = 0, ti = 0;
        int len=tlen-plen;
        while (pi < plen && ti-pi <= len) {
            if (textChars[ti] == patternChars[pi]) {
                ti++;
                pi++;
            } else {
                ti = ti - pi + 1;
                pi = 0;
            }
        }
        return (pi==plen)?(ti-pi):-1;
    }

}
